package com.fesine.netapply.dao.impl;

import com.fesine.netapply.dao.UserDao;
import com.fesine.netapply.util.StringUtil;
import com.fesine.netapply.vo.PageBean;
import com.fesine.netapply.vo.User;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository("userDao")
public class UserDaoImpl implements UserDao{
    @Resource
    private JdbcTemplate jdbcTemplate;

    public User login(User user) {
        String sql = "select * from t_user where userName=? and password=?";
        final User resultUser = new User();
        this.jdbcTemplate.query(sql, new Object[]{user.getUserName(), user.getPassword()}, new RowCallbackHandler() {
            public void processRow(ResultSet rs) throws SQLException {
                resultUser.setId(Integer.valueOf(rs.getInt("id")));
                resultUser.setUserName(rs.getString("userName"));
                resultUser.setPassword(rs.getString("password"));
            }
        });
        return resultUser;
    }

    public void add(User user) {
        String sql = "insert into t_user values(null,?,?,?)";
        this.jdbcTemplate.update(sql, new Object[]{user.getUserName(), user.getPassword(), user.getTrueName()});
    }

    public void update(User user) {
        String sql = "update t_user set userName=?,password=?,trueName=? where id=?";
        this.jdbcTemplate.update(sql, new Object[]{user.getUserName(), user.getPassword(), user.getTrueName(), user.getId()});
    }

    public void delete(int id) {
        String sql = "delete from t_user where id=?";
        this.jdbcTemplate.update(sql, new Object[]{Integer.valueOf(id)});
    }

    public List<User> find(PageBean pageBean, User user) {
        StringBuffer sb = new StringBuffer("select * from t_user t1 where 1=1");
        if(user != null && StringUtil.isNotEmpty(user.getUserName())) {
            sb.append(" and t1.userName like \'%" + user.getUserName() + "%\'");
        }

        if(pageBean != null) {
            sb.append(" limit " + pageBean.getStart() + "," + pageBean.getPageSize());
        }

        final ArrayList userList = new ArrayList();
        this.jdbcTemplate.query(sb.toString(), new Object[0], new RowCallbackHandler() {
            public void processRow(ResultSet rs) throws SQLException {
                User user = new User();
                user.setId(Integer.valueOf(rs.getInt("id")));
                user.setUserName(rs.getString("userName"));
                user.setPassword(rs.getString("password"));
                user.setTrueName(rs.getString("trueName"));
                userList.add(user);
            }
        });
        return userList;
    }

    public int count(User s_user) {
        StringBuffer sb = new StringBuffer("select count(*) from t_user");
        if(s_user != null && StringUtil.isNotEmpty(s_user.getUserName())) {
            sb.append(" and t1.userName like \'%" + s_user.getUserName() + "%\'");
        }

        return ((Integer)this.jdbcTemplate.queryForObject(sb.toString(), Integer.class)).intValue();
    }

    public User loadById(int id) {
        String sql = "select * from t_user t1 where t1.id=? ";
        final User user = new User();
        this.jdbcTemplate.query(sql, new Object[]{Integer.valueOf(id)}, new RowCallbackHandler() {
            public void processRow(ResultSet rs) throws SQLException {
                user.setId(Integer.valueOf(rs.getInt("id")));
                user.setUserName(rs.getString("userName"));
                user.setPassword(rs.getString("password"));
                user.setTrueName(rs.getString("trueName"));
            }
        });
        return user;
    }


}
